package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.common.vo.PatientSearch;
import com.gxa.common.vo.PatientVo;
import com.gxa.common.vo.TransCondition;
import com.gxa.entity.SysPatient;
import com.gxa.mapper.AppointmentManagementMapper;
import com.gxa.mapper.PatientManagementMapper;
import com.gxa.mapper.SysDiagnosisMapper;
import com.gxa.service.PatientManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientManagementServiceImpl implements PatientManagementService {


    @Autowired
    PatientManagementMapper patientManagementMapper;

    @Autowired
    AppointmentManagementMapper appointmentManagementMapper;

    @Autowired
    SysDiagnosisMapper sysDiagnosisMapper;



    int i = -1;

    /**
     * 查询患者列表
     * @param currentPage 当前页
     * @param pageSize 每一页显示条数
     * @return 患者列表
     */
    @Override
    public PageInfo<SysPatient> getPatientList(Integer currentPage, Integer pageSize) {


        PageHelper.startPage(currentPage,pageSize);
        List<Object> patientList = patientManagementMapper.selectPatientList();

        PageInfo<SysPatient> pageInfo = new PageInfo(patientList);

        System.out.println(pageInfo.getTotal());


        return pageInfo;
    }

    /**
     * 添加患者信息
     * @param sysPatient 患者对象
     * @return 添加成功或失败
     */
    @Override
    public R addPatient(SysPatient sysPatient) {

        R<Object> r = new R<>();
        SysPatient sysPatient1 = patientManagementMapper.selectActivePatientByIdCard(sysPatient.getIdCard());
        if (sysPatient1 == null) {
            patientManagementMapper.insertPatient(sysPatient);
            r.fromStatus(Status.SUCCESS);
        }else {
            r.setCode(10011);
            r.setMsg("该患者已存在于数据中心");
        }
        return r;
    }


    /**
     * 转交患者
     * @param transCondition 转交参数对象
     * @return 转交成功或失败
     */
    @Override
    public boolean transPatient(TransCondition transCondition) {

        List<Integer> patientIds = transCondition.getPatientIds();

        Integer userId = transCondition.getUserId();

        for (Integer patientId : patientIds) {

            i = patientManagementMapper.transPatient(patientId, userId);
        }
        return i>=0?true:false;
    }

    /**
     * 获取到患者信息
     * @param id 患者id
     * @return 患者对象
     */
    @Override
    public PatientVo getPatientById(Integer id) {

        PatientVo patientVo = patientManagementMapper.selectPatientById(id);

        return patientVo;
    }


    /**
     * 删除患者信息
     * @param id 患者id
     * @return 删除是否成功
     */
    @Override
    public R delPatient(Integer id) {

        R r = new R<>();

        if(id !=null && id>=0){
            //判断诊断中是否有患者
            Integer integer = sysDiagnosisMapper.countPatient(id);
            //判断预约中是否有患者
            Integer countByPatientCount = appointmentManagementMapper.getCountByPatientCount(id);
            if (integer ==0 || countByPatientCount ==0){
                Integer i = patientManagementMapper.deletePatient(id);
                if (i >0) {
                    r.fromStatus(Status.SUCCESS);
                    return R.ok();
                }else {
                    r.fromStatus(Status.SYSTEM_ERROR);
                }
            }else{
                r.setCode(10010);
                r.setMsg("该患者在预约或诊断中，无法删除");
            }
        }
        return r;
    }

    /**
     * 获取到高级筛选的患者信息
     * @param patientSearch 查询条件对象
     * @return 查询到患者列表
     */
    @Override
    public PageInfo getPatientListBySearch(PatientSearch patientSearch) {

        PageHelper.startPage(patientSearch.getCurrentPage(), patientSearch.getPageSize());
        List<PatientVo> objects = patientManagementMapper.selectPatientListBySearch(patientSearch);

        PageInfo pageInfo = new PageInfo<>(objects);

        return pageInfo;
    }

    /**
     * 通过名字筛选患者信息
     * @param name
     * @return
     */
    @Override
    public PageInfo<SysPatient> getPatientListByName(String name,Integer currentPage, Integer pageSize) {

        PageHelper.startPage(currentPage,pageSize);

        List<SysPatient> objects = patientManagementMapper.selectPatientByName(name);

        PageInfo<SysPatient> pageInfo = new PageInfo<>(objects);

        return pageInfo;
    }

    /**
     * 更新患者信息
     * @param sysPatient 患者对象
     * @return 是否成功
     */
    @Override
    public boolean updatePatient(SysPatient sysPatient) {
        int i = patientManagementMapper.updatePatient(sysPatient);
        return i>=0?true:false;
    }


}
